package user

import (
	"context"
	"errors"
	"hzjt-oam/rpc/sys/pb"
	"strconv"

	"hzjt-oam/admin/internal/svc"
	"hzjt-oam/admin/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserInfoLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUserInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserInfoLogic {
	return &UserInfoLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UserInfoLogic) UserInfo(req *types.UserInfoReq) (resp *types.UserInfoResp, err error) {
	// 这里的key和生成jwt token时传入的key一致

	req_id, err := strconv.Atoi(req.ID)
	if err != nil {
		return nil, errors.New("传入的id不是数值类型")
	}

	//userId, err := l.ctx.Value("userId").(json.Number).Int64()
	//if err != nil {
	//	return nil, errors.New("验证token未通过")
	//}

	user_one, err := l.svcCtx.UserSrv.GetSysUserById(l.ctx, &pb.GetSysUserByIdReq{
		Id: int64(req_id),
	})
	if err != nil {
		logx.WithContext(l.ctx).Errorf("根据userId: %s,查询用户异常:%s", strconv.FormatInt(int64(req_id), 10), err.Error())
		return nil, errors.New("查询用户失败")
	}

	return &types.UserInfoResp{
		Message:        "成功",
		Avatar:         user_one.SysUser.Avatar,
		Name:           user_one.SysUser.Name,
		NickName:       user_one.SysUser.NickName,
		Email:          user_one.SysUser.Email,
		Mobile:         user_one.SysUser.Mobile,
		Status:         user_one.SysUser.Status,
		DeptName:       user_one.SysUser.DeptName,
		CreateBy:       user_one.SysUser.CreateBy,
		CreateTime:     user_one.SysUser.CreateTime,
		LastUpdateBy:   user_one.SysUser.LastUpdateBy,
		LastUpdateTime: user_one.SysUser.LastUpdateTime,
		DelFlag:        user_one.SysUser.DelFlag,
		RoleName:       user_one.SysUser.RoleName,
	}, nil
}
